﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Net;
using System.IO;
using MonitorWin.PushService;



namespace MonitorWin
{

    public partial class MonitorDataService : ServiceBase
    {
        private PushServiceClient PushService = new PushServiceClient();
        private Timer aTimer;
        private int Delay = 60000;

        static void Main()
        {
            ServiceBase[] ServicesToRun = new ServiceBase[] { new MonitorDataService() };
            ServiceBase.Run(ServicesToRun);
        }
        public MonitorDataService()
        {
            InitializeComponent();

            aTimer = new Timer(Delay);
            aTimer.Elapsed += new ElapsedEventHandler(Timer_Tick);
            aTimer.Interval = Delay;
            aTimer.Enabled = false;
        }
        protected override void OnStart(string[] args)
        {
            aTimer.Enabled = true;
        }
        protected override void OnStop()
        {
            aTimer.Enabled = false;
        }
        private void Timer_Tick(object source, ElapsedEventArgs e)
        {
            try
            {
                WebRequest wReq = WebRequest.Create(@"http://www.bloomberg.com/markets/currencies/");
                WebResponse wResp = wReq.GetResponse();
                Stream respStream = wResp.GetResponseStream();
                StreamReader reader = new StreamReader(respStream, Encoding.ASCII);
                string respHTML = reader.ReadToEnd();

                int BeginPos = respHTML.IndexOf("<h2>Major World Currencies</h2>");
                int EndPos = respHTML.LastIndexOf("<div class='stock_chart'>") - BeginPos;
                respHTML = respHTML.Substring(BeginPos, EndPos);

                ParseItem(ref respHTML, "EUR-USD");
                ParseItem(ref respHTML, "GBP-USD");
                ParseItem(ref respHTML, "USD-JPY");
                ParseItem(ref respHTML, "AUD-USD");
                ParseItem(ref respHTML, "USD-CAD");
                ParseItem(ref respHTML, "USD-CHF");
                ParseItem(ref respHTML, "EUR-GBP");
                ParseItem(ref respHTML, "EUR-JPY");
                ParseItem(ref respHTML, "AUD-JPY");
            }
            catch (Exception ex)
            {

            }
        }
        private void ParseItem(ref string respHTML, string Currency)
        {
            //get value
            respHTML = respHTML.Substring(respHTML.IndexOf(Currency));
            respHTML = respHTML.Substring(8 + respHTML.IndexOf("'value'>"));
            double Value = Convert.ToDouble(respHTML.Substring(0, respHTML.IndexOf("</td>")).Replace('.', ','));

            //add to DB
            CreateCurrencyRateRequest CreateCurrencyRateRequest_Temp = new CreateCurrencyRateRequest();
            CreateCurrencyRateRequest_Temp.Period = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 
                DateTime.Now.Hour, DateTime.Now.Minute, 0);
            CreateCurrencyRateRequest_Temp.CurrencyName = Currency;
            CreateCurrencyRateRequest_Temp.Value = Value;
            PushService.AddCurencyRate(CreateCurrencyRateRequest_Temp);
        }

    }

}
